home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
Applications
/
Python 1.3.3
/
Python 133 68K
/
Lib
/
macbinary.py
< prev
next >
Wrap
Text File
|
1996-05-20
|
8KB
|
341 lines
"""macbinary - Macintosh binhex compression/decompression
easy interface:
encode(inputfilename, outputfilename)
decode(inputfilename, outputfilename)
"""
#
# Jack Jansen, CWI, October 1995.
# Adapted from binhex.py
#
# The module is supposed to be as compatible as possible. Especially the
# easy interface should work "as expected" on any platform.
# XXXX Note: currently, textfiles appear in mac-form on all platforms.
# We seem to lack a simple character-translate in python.
# (we should probably use ISO-Latin-1 on all but the mac platform).
# XXXX The simeple routines are too simple: they expect to hold the complete
# files in-core. Should be fixed.
# XXXX It would be nice to handle AppleDouble format on unix (for servers serving
# macs).
import sys
import os
import struct
import string
import binascii
DEBUG=0
if DEBUG:
testf=open('@macbinary.dbg.out', 'w')
Error = 'macbinary.Error'
# States (what have we written)
[_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
# Various constants
REASONABLY_LARGE=32768 # Minimal amount we pass the rle-coder
LINELEN=48 # What we pass to hqx-coder at once
# *NOTE* Must be divisible by 3!
RUNCHAR=chr(0x90) # run-length introducer
#
# The code is currently byte-order dependent
if struct.pack('i', 0177) != '\0\0\0\177':
raise ImportError, 'Module binhex is big-endian only'
#
# Workarounds for non-mac machines.
if os.name == 'mac':
import macfs
import MacOS
try:
openrf = MacOS.openrf
except AttributeError:
# Backward compatability
openrf = open
def FInfo():
return macfs.FInfo()
def getfileinfo(name):
finfo = macfs.FSSpec(name).GetFInfo()
dir, file = os.path.split(name)
# XXXX Get resource/data sizes
fp = open(name, 'rb')
fp.seek(0, 2)
dlen = fp.tell()
fp = openrf(name, '*rb')
fp.seek(0, 2)
rlen = fp.tell()
return file, finfo, dlen, rlen
def openrsrc(name, *mode):
if mode:
mode = mode[0]
else:
mode = 'rb'
mode = '*' + mode
return openrf(name, mode)
else:
#
# Glue code for non-macintosh useage
#
import regsub
class FInfo:
def __init__(self):
self.Type = '????'
self.Creator = '????'
self.Flags = 0
def getfileinfo(name):
finfo = FInfo()
# Quick check for textfile
fp = open(name)
data = open(name).read(256)
for c in data:
if not c in string.whitespace and (c<' ' or ord(c) > 0177):
break
else:
finfo.Type = 'TEXT'
fp.seek(0, 2)
dsize = fp.tell()
fp.close()
dir, file = os.path.split(name)
file = regsub.sub(':', '-', file)
return file, finfo, dsize, 0
class openrsrc:
def __init__(self, *args):
pass
def read(self, *args):
return ''
def write(self, *args):
pass
def close(self):
pass
class Encoder:
def __init__(self, (name, finfo, dlen, rlen), ofp):
if type(ofp) == type(''):
ofname = ofp
ofp = open(ofname, 'w')
if os.name == 'mac':
fss = macfs.FSSpec(ofname)
fss.SetCreatorType('BnHq', 'TEXT')
self.ofp = ofp
if finfo == None:
finfo = FInfo()
self.dlen = dlen
self.rlen = rlen
self.dtrailer = (dlen + 127) & 127
self.rtrailer = (rlen + 127) & 127
self._writeinfo(name, finfo)
self.state = _DID_HEADER
def _writeinfo(self, name, finfo):
if DEBUG:
print 'binhex info:', name, finfo.Type, finfo.Creator, self.dlen, self.rlen
name = name
nl = len(name)
if nl > 63:
raise Error, 'Filename too long'
d = '\0' + chr(nl) + name + '\0'*(63-nl)
d2 = finfo.Type + finfo.Creator + chr(finfo.Flags&0xff) + '\0\0\0\0\0\0\0' + '\0\0'
d3 = struct.pack('iiii', self.dlen, self.rlen, 0, 0)
d4 = '\0\0' + chr((finfo.Flags>>8) & 0xff)
info = d + d2 + d3 + d4
info = info + '\0'*(128-len(info))
self._write(info)
def _write(self, data):
self.ofp.write(data)
def write(self, data):
if self.state != _DID_HEADER:
raise Error, 'Writing data at the wrong time'
self.dlen = self.dlen - len(data)
self._write(data)
def close_data(self):
if self.dlen <> 0:
raise Error, 'Incorrect data size, diff='+`self.rlen`
self._write('\0'*self.dtrailer)
self.state = _DID_DATA
def write_rsrc(self, data):
if self.state < _DID_DATA:
self.close_data()
if self.state != _DID_DATA:
raise Error, 'Writing resource data at the wrong time'
self.rlen = self.rlen - len(data)
self._write(data)
def close(self):
if self.state < _DID_DATA:
self.close_data()
if self.state != _DID_DATA:
raise Error, 'Close at the wrong time'
if self.rlen <> 0:
raise Error, "Incorrect resource-datasize, diff="+`self.rlen`
self._write('\0'*self.rtrailer)
self.ofp.close()
self.state = None
def encode(inp, out):
"""(infilename, outfilename) - Create MacBinary-encoded copy of a file"""
finfo = getfileinfo(inp)
ofp = Encoder(finfo, out)
ifp = open(inp, 'rb')
# XXXX Do textfile translation on non-mac systems
d = ifp.read()
ofp.write(d)
ofp.close_data()
ifp.close()
ifp = openrsrc(inp, 'rb')
d = ifp.read()
ofp.write_rsrc(d)
ofp.close()
ifp.close()
class Decoder:
def __init__(self, ifp):
if type(ifp) == type(''):
ifp = open(ifp)
self.ifp = ifp
self._readheader()
def _read(self, length):
data = ''
while lenght:
newdata = self.ifp.read(length)
if not newdata:
raise Error, 'Premature end-of-file'
data = data + newdata
length = length - len(newdata)
return data
def _readheader(self):
first = self._read(1)
if first <> '\0':
raise Error, 'Not a MacBinary file'
len = self._read(1)
fname = self._read(63)
fname = fname[:len]
type = self._read(4)
creator = self._read(4)
flagsXXXXX Here I left off....
self._checkcrc()
type = rest[1:5]
creator = rest[5:9]
flags = struct.unpack('h', rest[9:11])[0]
self.dlen = struct.unpack('l', rest[11:15])[0]
self.rlen = struct.unpack('l', rest[15:19])[0]
if DEBUG:
print 'DATA, RLEN', self.dlen, self.rlen
self.FName = fname
self.FInfo = FInfo()
self.FInfo.Creator = creator
self.FInfo.Type = type
self.FInfo.Flags = flags
self.state = _DID_HEADER
def read(self, *n):
if self.state != _DID_HEADER:
raise Error, 'Read data at wrong time'
if n:
n = n[0]
n = min(n, self.dlen)
else:
n = self.dlen
self.dlen = self.dlen - n
return self._read(n)
def close_data(self):
if self.state != _DID_HEADER:
raise Error, 'close_data at wrong time'
if self.dlen:
dummy = self._read(self.dlen)
self._checkcrc()
self.state = _DID_DATA
def read_rsrc(self, *n):
if self.state == _DID_HEADER:
self.close_data()
if self.state != _DID_DATA:
raise Error, 'Read resource data at wrong time'
if n:
n = n[0]
n = min(n, self.rlen)
else:
n = self.rlen
self.rlen = self.rlen - n
return self._read(n)
def close(self):
if self.rlen:
dummy = self.read_rsrc(self.rlen)
self._checkcrc()
self.state = _DID_RSRC
self.ifp.close()
def hexbin(inp, out):
"""(infilename, outfilename) - Decode binhexed file"""
ifp = HexBin(inp)
finfo = ifp.FInfo
if not out:
out = ifp.FName
if os.name == 'mac':
ofss = macfs.FSSpec(out)
out = ofss.as_pathname()
ofp = open(out, 'wb')
# XXXX Do translation on non-mac systems
d = ifp.read()
ofp.write(d)
ofp.close()
ifp.close_data()
d = ifp.read_rsrc()
if d:
ofp = openrsrc(out, 'wb')
ofp.write(d)
ofp.close()
if os.name == 'mac':
nfinfo = ofss.GetFInfo()
nfinfo.Creator = finfo.Creator
nfinfo.Type = finfo.Type
nfinfo.Flags = finfo.Flags
ofss.SetFInfo(nfinfo)
ifp.close()
def _test():
if os.name == 'mac':
fss, ok = macfs.PromptGetFile('File to convert:')
if not ok:
sys.exit(0)
fname = fss.as_pathname()
else:
fname = sys.argv[1]
#binhex(fname, fname+'.hqx')
#hexbin(fname+'.hqx', fname+'.viahqx')
hexbin(fname, fname+'.unpacked')
sys.exit(1)
if __name__ == '__main__':
_test()